package com.meichis.yndms.ui.activity;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.ProductStockAdapter;
import com.meichis.yndms.databinding.ActivityProductStockBinding;
import com.meichis.yndms.model.BusinessBoard;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.ProductStockVM;

import java.util.ArrayList;

public class ProductStockActivity extends BaseActivity {

    private ActivityProductStockBinding binding;
    private ProductStockVM vm;
    private ProductStockAdapter adapter;


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_product_stock);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ProductStockVM.class);
        binding.setClick(new ClickEvent());
        binding.setVm(vm);
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("库存现有量");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());

        adapter = new ProductStockAdapter(this, R.layout.item_product_stock);
        binding.rc.setAdapter(adapter);
        binding.rc.setLayoutManager(new LinearLayoutManager(this));

        vm.getMonthProductSalesSummaryByBrand();

        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getAllDatas().observe(this, datas -> {
            if (datas == null || datas.size() < 1) {
                return;
            }

            for (BusinessBoard board : datas) {
                if (vm.getMapBusinessBoard().getValue() == null || !vm.getMapBusinessBoard().getValue().containsKey(board)) {
                    vm.getMapBusinessBoard().getValue().put(board, new ArrayList<>());
                    vm.getBusinessBoardList().getValue().add(board);
                }
                vm.getMapBusinessBoard().getValue().get(board).add(board);
            }



            vm.getBusinessBoard().setValue(vm.getBusinessBoardList().getValue().get(0));
            adapter.setList(vm.getMapBusinessBoard().getValue().get(vm.getBusinessBoard().getValue()));

        });
    }


    public class ClickEvent {

        public void onSelectWarehouse() {
            new DropDownDialog<>(ProductStockActivity.this, R.color.theme, "选择仓库", "StorageName", vm.getBusinessBoardList().getValue(), (o, position) -> {
                vm.getBusinessBoard().setValue(vm.getBusinessBoardList().getValue().get(position));
                adapter.setList(vm.getMapBusinessBoard().getValue().get(vm.getBusinessBoard().getValue()));
            }).show();
        }
    }
}
